package io.lkt.modules.sys.controller;

import io.lkt.common.util.Query;
import io.lkt.common.utils.DateUtil;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.modules.sys.entity.BDiscountCouponEntity;
import io.lkt.modules.sys.entity.SysGzhEntity;
import io.lkt.modules.sys.entity.SysUserEntity;
import io.lkt.modules.sys.entity.SysUserGzhEntity;
import io.lkt.modules.sys.service.BDiscountCouponService;
import java.io.PrintStream;
import java.util.List;
import java.util.Map;

import io.lkt.modules.sys.service.SysUserRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;







@CrossOrigin
@RestController
@RequestMapping({"/sys/coupon"})
public class BDiscountCouponController
  extends AbstractController
{
  @Autowired
  private BDiscountCouponService bDiscountCouponService;

  @Autowired
  SysUserRoleService sysUserRoleService;
  
  @RequestMapping({"/couponList"})
  @RequiresPermissions({"sys:buserinfo:list"})
  public R couponList(@RequestParam Map<String, Object> params)
  {
    SysUserEntity curUser = getUser();
    List<SysUserGzhEntity> userGzhList = this.sysUserRoleService.queryGzhList(curUser.getUserId());
    params.put("userGzhList", userGzhList);
    Query query = new Query(params);
     List<BDiscountCouponEntity> discountCouponEntities = this.bDiscountCouponService.selectCouponListAll(query);
     int total = this.bDiscountCouponService.queryComplainTotal(query).intValue();
    PageUtils pageUtil = new PageUtils(discountCouponEntities, total, query.getLimit(), query.getPage());
    return R.ok().put("page", pageUtil);
  }
  
  @RequestMapping({"/couponById"})
  @RequiresPermissions({"sys:buserinfo:list"})
  public R couponById(@RequestBody Integer id) {
    BDiscountCouponEntity bDiscountCouponEntity = this.bDiscountCouponService.couponById(id);
    return R.ok().put("coupon", bDiscountCouponEntity);
  }
  




  @RequestMapping({"/delCoupon"})
  @RequiresPermissions({"sys:buserinfo:list"})
  public R delCoupon(@RequestBody Integer id)
  {
   this.bDiscountCouponService.delCouponById(id);
    return R.ok();
  }
  





  @RequestMapping({"/addCoupon"})
  @RequiresPermissions({"sys:buserinfo:list"})
  public R addCoupon(@RequestBody BDiscountCouponEntity bDiscountCouponEntity)
  {
    SysUserEntity curUser = getUser();
    bDiscountCouponEntity.setId(null);
    bDiscountCouponEntity.setCreatorUserId(Integer.valueOf(curUser.getUserId().intValue()));
    bDiscountCouponEntity.setCreateTime(DateUtil.getCurDate());
    bDiscountCouponEntity.setModifyTime(bDiscountCouponEntity.getCreateTime());
    this.bDiscountCouponService.addCoupon(bDiscountCouponEntity);
    return R.ok();
  }
  





  @RequestMapping({"/updateCoupon"})
  @RequiresPermissions({"sys:buserinfo:list"})
  public R updateCoupon(@RequestBody BDiscountCouponEntity bDiscountCouponEntity)
  {
    bDiscountCouponEntity.setModifyTime(DateUtil.getCurDate());
    this.bDiscountCouponService.updateCoupon(bDiscountCouponEntity);
    return R.ok();
  }
  





  @RequestMapping({"/selectGzhCode"})
  @RequiresPermissions({"sys:buserinfo:list"})
  public R selectGzhCode()
  {
    List<SysGzhEntity> gzhEntities = this.bDiscountCouponService.selectGzhCode();
    return R.ok().put("gzhEntities", gzhEntities);
  }
}
